package com.shimakaze.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shimakaze.reggie.common.BaseContext;
import com.shimakaze.reggie.common.R;
import com.shimakaze.reggie.dto.OrdersDto;
import com.shimakaze.reggie.entity.OrderDetail;
import com.shimakaze.reggie.entity.Orders;
import com.shimakaze.reggie.service.OrderDetailService;
import com.shimakaze.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @Autowired
    OrderDetailService orderDetailService;

    /**
     * 用户下单，已经登陆了所以可以根据用户id查询购物车信息
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("下单成功");
    }


    /**
     * 后台查询用户订单
     * @param page
     * @param pageSize
     * @param number //订单号
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime){

        //分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);

        //条件构造器
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件  动态sql  字符串使用StringUtils.isNotEmpty这个方法来判断
        ordersLambdaQueryWrapper.like(StringUtils.isNotEmpty(number),Orders::getNumber,number)
                .gt(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime,beginTime)
                .lt(StringUtils.isNotEmpty(endTime),Orders::getOrderTime,endTime);

        orderService.page(ordersPage,ordersLambdaQueryWrapper);

        return R.success(ordersPage);

    }


    /**
     * 修改订单状态  1待付款，2待派送，3已派送，4已完成，5已取消
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> updateOrderStatus(@RequestBody Orders orders){
        //order的主键id和订单号number的值相同
        Long id = orders.getId();
        Integer status = orders.getStatus();

        if(id == null || status==null){
            return R.error("传入信息不合法");
        }
        Orders orders1 = orderService.getById(id);
        orders1.setStatus(status);
        orderService.updateById(orders1);
        return R.success("修改订单状态成功");

    }


    /**
     * 移动端用户查询订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize){
        // 分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        // Orders条件构造器
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        ordersLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(ordersPage,ordersLambdaQueryWrapper);


        // OrdersDetail条件构造器
        LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();

        List<Orders> ordersList = ordersPage.getRecords();
        List<OrdersDto> ordersDtoList = ordersList.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            //订单id
            Long orderId = item.getId();
            List<OrderDetail> orderDetailList = orderDetailService.getOrderDetailListByOrderId(orderId);
            BeanUtils.copyProperties(item,ordersDto);
            //对orderDto进行OrderDetails属性的赋值
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;

        }).collect(Collectors.toList());

        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");
        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersDtoPage);
    }


}
